package com.example.Blog.controller;

import com.example.Blog.model.Comment;
import com.example.Blog.model.User;
import com.example.Blog.service.CommentService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController {
    final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    // 发布评论
    @PostMapping
    public String postComment(Comment comment, HttpServletRequest httpServletRequest) {
        User loginUser = (User) httpServletRequest.getSession().getAttribute("loginUser");
        comment.setCommentTime(new Date());
        comment.setCommentBelongToUser(loginUser.getUserId());
        comment.setCommentBelongToUserAccount(loginUser.getUserAccount());
        comment.setCommentBelongToUserName(loginUser.getUserNickname());
        this.commentService.postComment(comment);
        return "redirect:/article/detail?articleId="+comment.getCommentBelongToArticle();
    }

    @GetMapping("/deleteComment")
    public String deleteComment(@RequestParam int commentId, @RequestParam int articleId){
        // TODO: 2023/12/1 这里也应该加入验证 
        this.commentService.deleteComment(commentId);
        return "redirect:/article/detail?articleId="+articleId;
    }
}
